<?php

class Department Extends MY_Controller
{
	public function view()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->load->model('M_department','md');
		$this->load->model('M_position','mp');
		$this->load->library('pagination');
		
		$config['base_url'] = base_url().'department/view';
		$config['total_rows'] = $this->md->count_dept();
		$config['per_page'] = 10; 
		
		//Pagnate Design start
			$config['full_tag_open'] = '<div class="pagination pagination-centered"><ul>';
			$config['full_tag_close'] = '</ul></div>';
			
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			
			$config['cur_tag_open'] = '<li class="active"><a href="#">';
			$config['cur_tag_close'] = '</a></li>';
			
			$config['prev_link'] = '&lt;';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			
			$config['next_link'] = '&gt;';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			
			$config['first_link'] = 'First';
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			
			$config['last_link'] = 'Last';
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			
		//Pagnate Design end
		
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->pagination->initialize($config); 
		$this->view_data['departments'] = $this->md->fetch_dept($config['per_page'] ,$page);
		$this->view_data['pagnate_links'] = $this->pagination->create_links();
		$this->view_data['show_positions'] = function($x){return $this->mp->get_positions_by_dept_id($x);};
	}
	
	public function add()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->load->library('form_validation');
		$this->load->helper('form');
		
		if($this->input->post('add_department'))
		{
			$this->form_validation->set_rules('name','Department Name','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('description','Department Description','trim|htmlspecialchars');

			if($this->form_validation->run() !== FALSE)
			{
				$this->load->model('M_department','md');
				if($this->md->insert($this->input->post()))
				{
					$this->_msg('s','Department Successfully Added','department/add');
				}else
				{
					$this->view_data['system_message'] = $this->_msg('e','Department Was Not added');
				}
			}
		}
	}
	
	public function update()
	{
		
	
	}
	
	public function delete()
	{
	
	
	
	
	}
	
	public function positions($id)
	{
		if(is_numeric($id))
		{
			$this->load->model('M_department','md');
			$this->load->model('M_position','mp');
			$this->load->library(array('form_validation'));
			$this->load->helper(array('form'));
			
			$this->view_data['system_message'] = $this->_msg();
			$this->view_data['department'] = $this->md->get($id);
			$this->view_data['positions'] = $this->mp->get_positions_by_dept_id($id);
			$this->view_data['deptartment_id'] = $id;
			
		
			if($this->input->post('add_position'))
			{
				$this->form_validation->set_rules('name','Name of position','requried|trim"htmlspecialchars');
				$this->form_validation->set_rules('description','Description','trim"htmlspecialchars');
				
				$input['position_name'] = $this->input->post('name');
				$input['position_desc'] = $this->input->post('description');
				$input['dept_id'] = $id;
				$input['created_at'] = NOW;
				
				$this->load->model('m_position','mp');
				if($this->mp->insert($input))
				{
					$this->_msg('s','New Position Was successfully added to the department.','department/view');
				}else{
					$this->view_data['system_message'] = $this->_msg('e','An error was encountered while adding a new position');
				}
			}
		}else{
			redirect('department/view');
		}
	}
}